package cn.kone.blog.controller;


import cn.kone.base.utlis.R;
import cn.kone.blog.domain.Comment;
import cn.kone.blog.service.ICommentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类: 评论Comment
 */
@RestController
@RequestMapping("/comment")
@Api(tags = "评论接口类")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    /**
     * 接口: 根据文章id查询当前文章的所有评论
     * @comment articleId
     * @return
     */
    @GetMapping("/{articleId}")
    public List<Comment> getComment(@PathVariable("articleId")Long articleId){
        return commentService.findByArticleId(articleId);
    }


    /**
     * 接口: 添加和修改
     * @param comment
     * @return
     */
    @PutMapping
    public R<Void> addOrUpdate(@RequestBody Comment comment){
        try {
            if(comment.getId() == null){//添加
                commentService.add(comment);
            }else{
                commentService.update(comment);
            }
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
    }
}
